package priv.wangy.micro.common.sync.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 *@author wangy
 *@since 2024/12/2
 */
@Data
@ConfigurationProperties(prefix = "data-sync")
public class DataSyncProperties {

    private List<ProducerParams> producers = new ArrayList<>();

    private List<ConsumerParams> consumers = new ArrayList<>();

    @Data
    public static class ProducerParams {

        private String name;

        private String producer;
    }

    @Data
    public static class ConsumerParams {

        private String name;

        private String consumer;
    }

    public ProducerParams getProducer(String name) {
        if (!CollectionUtils.isEmpty(producers)) {
            return producers.stream().filter(producer -> Objects.equals(producer.getName(), name)).findFirst().orElse(null);
        }
        return null;
    }

    public ConsumerParams getConsumer(String name) {
        if (!CollectionUtils.isEmpty(consumers)) {
            return consumers.stream().filter(consumer -> Objects.equals(consumer.getName(), name)).findFirst().orElse(null);
        }
        return null;
    }
}
